package com.example.brazil_project.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.database.Cursor;
import android.util.Log;

import com.example.brazil_project.parameter.Parameter;
/**
 * 获取系统信息的线程
 * @author charlie1
 *
 */
public class SystemMsgUtil{
	
//	public static boolean isrun = false;
//	public static SystemMsgUtil systemmsgutil = null;
//	private Activity Contxt = null;
//	
//
//	public SystemMsgUtil(){
//		this.Contxt = Parameter.openingContx;
//		
//	}
//	public void run(){
//		getSystemMsg();
//	}
////	public static SystemMsgUtil getSystemMsgThread() {
////		if(systemmsgutil==null){
////			systemmsgutil = new SystemMsgUtil();
////		}
////		return systemmsgutil;
////	}

	
	public void getSystemMsg(){

			Activity Contxt = Parameter.openingContx;
			List list1 = new ArrayList();
			list1.add("username");
			Map <String,String> map1 =  SharedPreferencesUtil.getSharedPreferencesData( Contxt, "u_p", list1) ;
			String  username = map1.get("username");
			String First_Time = Parameter.getFirst_Time();
		
			//没有登录则不跑后台线程
				if(username==null||username.length()<=0){			
					return;
				}
				
				 List<NameValuePair> params = new ArrayList<NameValuePair>();  
			     params.add(new BasicNameValuePair("create_date",First_Time));  
			     params.add(new BasicNameValuePair("username",username)); 
			     
			     //获取当前本地保存系统信息的最大ID
			     String selectsql = "SELECT max(id) as maxid FROM SystemInform_table WHERE  username ='"+username+"' and type ='1'";
			     Cursor c1 = SQLiteDatabaseUtil.select(selectsql);
				 if(c1!=null&&c1.getCount()>0){
					 while (c1.moveToNext()) {  
						  int maxid = c1.getInt(c1.getColumnIndex("maxid"));
						  params.add(new BasicNameValuePair("sysmaxid",maxid+"")); 
				              }  
				 }
				 
//				 selectsql = "SELECT type , id FROM SystemInform_table WHERE  username ='"+username+"'";
//			     Cursor c30 = SQLiteDatabaseUtil.select(selectsql);
//				 
//			     if(c30!=null&&c30.getCount()>0){
//					 while (c30.moveToNext()) {  
//						  int maxid = c30.getInt(c30.getColumnIndex("id"));
//						  String type1= c30.getString(c30.getColumnIndex("type"));
//						  System.out.println(type1);
//				              }  
//				 }
			     //获取当前本地保存反馈信息的最大ID
			     selectsql = "SELECT max(id) as maxid FROM SystemInform_table WHERE  username ='"+username+"' and type ='2'";
			     Cursor c2 = SQLiteDatabaseUtil.select(selectsql);
				 if(c2!=null&&c2.getCount()>0){
					 while (c2.moveToNext()) {  
						  int maxid = c2.getInt(c2.getColumnIndex("maxid"));
						  params.add(new BasicNameValuePair("replymaxid",maxid+"")); 
				              }  
				 }	
			     String result = HttpUtil.doPost(Parameter.getServerPath()+"APP/msg/getSystemInform.nut",params);
				 List<Map<String, Object>>list = jsonUtil.getlistForJson(result);
				 //存放系统信息
				 String sysmsg =null;
				 //存放反馈信息
				 String yjfkmsg =null;
				 if(list!=null&&list.size()>0){
					 for (Map map : list) {
						 selectsql = "SELECT * FROM SystemInform_table WHERE id = "+map.get("id")+" AND username ='"+username+"'";
						 Cursor c3 = SQLiteDatabaseUtil.select(selectsql);
						 //如果这条系统系统信息已经传给这位客户了，就不需要再传给他们
						 if(c3!=null&&c3.getCount()>0){
							 
							 continue;
						 }
						 
						 String insertSql = "INSERT INTO SystemInform_table VALUES ("+map.get("id")+",'"+map.get("title")+"','"+map.get("content")+"','"+map.get("CreateDate")+"'"
						 		+ ",'"+username+"','"+0+"','"+map.get("msgType")+"','"+map.get("reply")+"')";
						 SQLiteDatabaseUtil.insert(insertSql);
						 
						 //如果有系统消息提示，一次多条也只发一次信息
						 if(sysmsg ==null&&map.get("msgType").toString().equals("1")){
							 SendMsgUtil.sengNotification(Contxt, map.get("title").toString(), map.get("content").toString(), null,"assets/img/msg_xt.png",1);
							 sysmsg = "sent";
						 }
						 if(yjfkmsg ==null&&map.get("msgType").toString().equals("2")){
							 SendMsgUtil.sengNotification( Contxt, map.get("title").toString(), map.get("content").toString(), null,"assets/img/msg_xt.png",1);
							 yjfkmsg = "sent";
						 }	
						 }

				 }
				 Log.e("sysmsg", "sysmsg");
				
			
			
		
	
	}
}
